package org.testis.testdriver.translation;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Vector;

/**
 *
 * @author barisergun
 */
public class AtofValueTranslateService {

    private HashMap<String, String> m_testplanglobals;
    private HashMap<String, String> m_testplanlanguages;
    private HashMap<String, String> m_testplandutvalues;
    private HashMap<String, String> m_usemapglobals;

    public AtofValueTranslateService(HashMap<String, String> testplanglobals,
            HashMap<String, String> testplanlanguages,
            HashMap<String, String> testplandutvalues,
            HashMap<String, String> usemapglobals) {
        m_testplanglobals = testplanglobals;
        m_testplanlanguages = testplanlanguages;
        m_testplandutvalues = testplandutvalues;
        m_usemapglobals = usemapglobals;
    }

    public String translateValue(String valueToTranslate) {
        valueToTranslate = translateStaticVariables(valueToTranslate);
        valueToTranslate = translateRuntimeVariables(valueToTranslate);
        return valueToTranslate;
    }

    public String translateRuntimeVariables(String valueToTranslate) {
        Vector<AtofValueTranslateAction> actionVector =
                new Vector<AtofValueTranslateAction>();



        if (valueToTranslate.contains(AtofTranslationConstants.USEMAP_CONSTANT)) {
            actionVector.add(new AtofUseMapTranslateAction(m_usemapglobals));
        }

        if (!actionVector.isEmpty()) {

            for (int y = 0; y < actionVector.size() - 1; y++) {

                actionVector.get(y).setSuccessor(actionVector.get(y + 1));

            }
            valueToTranslate = actionVector.get(0).processTranslation(valueToTranslate);

        }

        return valueToTranslate;
    }

    public String translateStaticVariables(String valueToTranslate) throws UnidentifiedTranslationException {
        Vector<AtofValueTranslateAction> actionVector =
                new Vector<AtofValueTranslateAction>();

        actionVector.add(new AtofLanguageTranslateAction(m_testplanlanguages));
        actionVector.add(new AtofGlobalTranslateAction(m_testplanglobals));
        actionVector.add(new AtofDutValueTranslateAction(m_testplandutvalues));

        /*if (valueToTranslate.contains(AtofTranslationConstants.LANGUAGE_CONSTANT)) {
            actionVector.add(new AtofLanguageTranslateAction(m_testplanlanguages));
        }
        if (valueToTranslate.contains(AtofTranslationConstants.GLOBAL_CONSTANT)) {
            actionVector.add(new AtofGlobalTranslateAction(m_testplanglobals));
        }
        if (valueToTranslate.contains(AtofTranslationConstants.DUT_VALUE_CONSTANT)) {
            actionVector.add(new AtofDutValueTranslateAction(m_testplandutvalues));
        }*/

        if (!actionVector.isEmpty()) {

            for (int y = 0; y < actionVector.size() - 1; y++) {

                actionVector.get(y).setSuccessor(actionVector.get(y + 1));

            }
            
            while (staticVariableExists(valueToTranslate)) {
                valueToTranslate = actionVector.get(0).processTranslation(valueToTranslate);
                
            }


            HashMap<String,String> exceptions = new HashMap<String,String>();
            for (int y = 0; y < actionVector.size(); y++) {

                exceptions.putAll(actionVector.get(y).getExceptions());

            }


            if (!exceptions.isEmpty()) {
                StringBuilder ecpBuilder = new StringBuilder();
                for(Iterator<Entry<String,String>> it =
                        exceptions.entrySet().iterator(); it.hasNext();)
                {
                    Entry<String,String> found  = it.next();
                    ecpBuilder.append(found.getValue());
                    ecpBuilder.append('\n');
                }

                throw new UnidentifiedTranslationException(ecpBuilder.toString());
            }

        }

        return valueToTranslate;
    }

    private Boolean staticVariableExists(String valueToTranslate) {

        StringBuilder sample = new StringBuilder(valueToTranslate);

        if (valueToTranslate.contains(AtofTranslationConstants.LANGUAGE_CONSTANT)) {
            return true;
        }
        if (valueToTranslate.contains(AtofTranslationConstants.GLOBAL_CONSTANT)) {
            return true;
        }
        if (valueToTranslate.contains(AtofTranslationConstants.DUT_VALUE_CONSTANT)) {
            return true;
        }
        return false;

    }
}
